<template>
  <div class="im-c" :class="{ hideSendTool: !canSendMsg }">
    <im-top v-if="showImTop"></im-top>
    <!-- 聊天信息 -->
    <section class="im-m" @touchstart="pickUpKeyboard">
      <van-list
        id="im-m"
        v-model="isLoading"
        class="van-pull-refresh-custom"
        :class="{ showBottom: showBottom }"
        :finished="isFinished"
        :immediate-check="false"
        :offset="10"
        direction="up"
      >
        <template v-for="(msg, idx) in msgList">
          <div style="height: 100px" :key="idx"></div>
        </template>
      </van-list>
    </section>
    <!-- 输入 -->
    <operator-area
      v-if="canSendMsg"
      ref="operator"
      @sendMsg="sendMsg"
      @toggleBottom="toggleBottom"
    />
  </div>
</template>

<script>
// import * as uuid from 'uuid';
import OperatorArea from './components/operator-area.vue';
import ImTop from './components/im-top.vue';

export default {
  components: { OperatorArea, ImTop },
  data() {
    return {
      msgList: [],
      showEmoji: false,
      isLoading: false,
      isFinished: false,
      showBottom: false,
      canSendMsg: true,
      showImTop: true
    };
  },
  mounted() {},
  beforeDestroy() {},
  methods: {
    toggleBottom(bool) {
      this.showBottom = bool;
      if (bool) {
        this.scrollToBottom();
      }
    },
    /* 滚动到底部 */
    scrollToBottom() {
      this.$nextTick(() => {
        setTimeout(() => {
          const container = document.getElementById('im-m');
          if (container) {
            /* 加一个底部栏的高度 */
            container.scrollTop = container.scrollHeight + 49;
          }
        }, 200);
      });
    },
    sendMsg() {},
    pickUpKeyboard() {
      if (this.$refs.operator) {
        this.$refs.operator.pickUpKeyboard();
        if (this.$refs.operator.$refs && this.$refs.operator.$refs.IMInput) {
          this.$refs.operator.$refs.IMInput.blur();
        }
      }
    }
  }
};
</script>

<style lang="scss" scoped>
.im-c {
  width: 100vw;
  height: 100vh;
  background-color: #f5f5f5;
  padding-bottom: 49px;
  box-sizing: border-box;
  overflow: hidden;
  &.hideSendTool {
    padding-bottom: 0;
  }
  .im-m {
    width: 100%;
    height: 100%;
    position: fixed;
    top: 0;
    left: 0;
    overflow-y: scroll;

    .van-pull-refresh-custom {
      width: 100%;
      height: 100%;
      box-sizing: border-box;
      padding-bottom: 49px;
      transition: padding-bottom 0.2s;
      display: block;
      overflow-y: scroll;
      &.showBottom {
        padding-bottom: 219px;
      }
    }
  }
}
</style>

<style lang="scss">
.im-c {
  .van-overlay {
    background-color: rgba(0, 0, 0, 0.6);
  }
}
</style>
